package com.example.logintest.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.logintest.annotation.UserLoginToken;
import com.example.logintest.common.CodeMsg;
import com.example.logintest.common.Result;
import com.example.logintest.pojo.User;
import com.example.logintest.service.IUserService;
import com.example.logintest.service.impl.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Lemon
 * @Description: TODO
 * @Date: 2024/2/18 13:59
 */
@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private IUserService userService;

    @Resource
    private TokenService tokenService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询用户信息
     *
     * @return
     */
    @UserLoginToken
    @GetMapping("/list")
    @ResponseBody
    public Result<Object> list() {
        return Result.success(userService.list());
    }


    /**
     * 登录验证
     *
     * @param user
     * @param response
     * @return
     */
    @GetMapping("/login")
    @ResponseBody
    public Result<Object> login(User user, HttpServletResponse response) {
        Map<String, Object> jsonObject = new HashMap();
        //获取用户账号密码
        User userForBase = new User();
        User byUsername = userService.findByUsername(user);
        if (byUsername==null) {
            return Result.error(CodeMsg.USER_NOT_EXSIST);
        }
            userForBase.setId(byUsername.getId());
            userForBase.setUsername(byUsername.getUsername());
            userForBase.setPassword(byUsername.getPassword());
        //判断账号或密码是否正确
        if (!userForBase.getPassword().equals(user.getPassword())) {
            return Result.error(CodeMsg.USER_OR_PASS_ERROR);
        } else {
            String token = tokenService.getToken(userForBase);
            jsonObject.put("token", token);
            Cookie cookie = new Cookie("token", token);
            String redisKey = "token:"+token;
            stringRedisTemplate.opsForValue().set(redisKey,token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return Result.success(jsonObject);
        }
    }

    /**
     * 注册账号
     *
     * @param user
     * @param
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public Result<Object> register(@RequestBody User user) {
        Map<String, Object> jsonObject = new HashMap();
        User byUsername = userService.findByUsername(user);
        if (byUsername!=null) {
            return Result.error(CodeMsg.USER_EXSIST);
        }
        userService.addUser(user);
        jsonObject.put("describe","注册成功");
        return Result.success(jsonObject);
    }
}
